<html>
<head><meta charset="utf-8"><title>type param provided where const was expected · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html">type param provided where const was expected</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225967337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225967337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225967337">(Feb 11 2021 at 09:41)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/81985">#81985</a></p>



<a name="225967521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225967521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225967521">(Feb 11 2021 at 09:43)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="3805">@project-const-generics</span></p>
<p>similar to how we currently recommend wrapping the argument in braces we can also recommend changing the type parameter to a const parameter in case there is a mismatch. This happens here <a href="https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47">https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47</a></p>



<a name="225967661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225967661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225967661">(Feb 11 2021 at 09:45)</a>:</h4>
<p>we can do the same if the path is resolved to an error instead of a param to fix the following: (getting the right span for the suggestion is a bit harder though)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="225967775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225967775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225967775">(Feb 11 2021 at 09:47)</a>:</h4>
<p>for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>you would have to look where we emit the "expected value, found type parameter" error and recommend changing the type param to a const parameter in the same way</p>



<a name="225967814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225967814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225967814">(Feb 11 2021 at 09:47)</a>:</h4>
<p>i think at least for now it's fine to not check if the param is also used as an actual type parameter somewhere else</p>



<a name="225973712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225973712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225973712">(Feb 11 2021 at 10:50)</a>:</h4>
<p>one issue i didn't consider is which type to recommend for the const param</p>



<a name="225973745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225973745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225973745">(Feb 11 2021 at 10:50)</a>:</h4>
<p>i think for the error in <a href="https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47">https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47</a> we can use the param type</p>



<a name="225973799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/225973799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#225973799">(Feb 11 2021 at 10:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected/near/225967775">said</a>:</p>
<blockquote>
<p>for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>you would have to look where we emit the "expected value, found type parameter" error and recommend changing the type param to a const parameter in the same way</p>
</blockquote>
<p>here we maybe have to be more careful and possible start by recommending <code>const N: ExpectedType</code> or something</p>



<a name="226028700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226028700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226028700">(Feb 11 2021 at 17:38)</a>:</h4>
<p>For cases like these we can leverage the info the compiler has to expect the right type (in this case <code>const N: u8</code>)<br>
Also, I think that nowadays we don't really resolve <code>N</code> to figure out _what the type is_, just that it comes from the TypeNS.</p>



<a name="226209583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226209583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226209583">(Feb 12 2021 at 23:17)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="3805">@project-const-generics</span> does one of you want to work on this?</p>



<a name="226209727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226209727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226209727">(Feb 12 2021 at 23:18)</a>:</h4>
<blockquote>
<p>Also, I think that nowadays we don't really resolve <code>N</code> to figure out _what the type is_, just that it comes from the TypeNS.</p>
</blockquote>
<p>not sure what you are replying to here, resolving <code>N</code> to figure out _what the type is_ would just return <code>ty::Param</code> anyways, wouldn't it?</p>



<a name="226209837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226209837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226209837">(Feb 12 2021 at 23:19)</a>:</h4>
<p>for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>and</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>we know the expected type because it has to be equal to the type of the generic param we're instantiating</p>



<a name="226210030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226210030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226210030">(Feb 12 2021 at 23:21)</a>:</h4>
<p>for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Display</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>we probably have to typeck <code>{ N }</code> and then look at the typeck results for the <code>HirId</code> of the path. Idk if that actually works though as the type could just be <code>ty::Error</code> <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> is that what you were talking about <span class="user-mention" data-user-id="119031">@Esteban Küber</span></p>



<a name="226213032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226213032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226213032">(Feb 13 2021 at 00:00)</a>:</h4>
<p>If no one else wants to take it, I can take it time-permitting</p>



<a name="226213797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226213797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226213797">(Feb 13 2021 at 00:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected/near/225973745">said</a>:</p>
<blockquote>
<p>i think for the error in <a href="https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47">https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47</a> we can use the param type</p>
</blockquote>
<p>I'm a bit confused because at this point we're still only at the HIR, and the TyKind::Param is on the <code>rustc_middle::TyKind</code> I believe</p>
<p>edit: or did you mean the struct <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamTy.html">ParamTy</a></p>



<a name="226214580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226214580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226214580">(Feb 13 2021 at 00:21)</a>:</h4>
<p>also just to make sure I have it clear we'd recommend:<br>
<code>impl&lt;N&gt; Display for A&lt;N&gt; </code> --&gt; <code>impl&lt;*const N: SomeType*&gt; Display for A&lt;N&gt;</code></p>



<a name="226215597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226215597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226215597">(Feb 13 2021 at 00:36)</a>:</h4>
<p>What I meant was that for <code>impl&lt;N&gt; Display for A&lt;N&gt;</code> <code>N</code> will be of ty::Param, while in <code>impl Display for A&lt;N&gt;</code> it will be of ty::Error</p>



<a name="226215607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226215607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Esteban Küber <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226215607">(Feb 13 2021 at 00:37)</a>:</h4>
<p>Poorly phrased in my part <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="226215622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226215622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226215622">(Feb 13 2021 at 00:37)</a>:</h4>
<p>nah y'all good, I'm still quite unfamiliar with the terminology so just want to make sure I have it absolutely clear what is meant</p>



<a name="226232946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/type%20param%20provided%20where%20const%20was%20expected/near/226232946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected.html#226232946">(Feb 13 2021 at 07:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="326189">jknodt</span> <a href="#narrow/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected/near/226213797">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/260443-project-const-generics/topic/type.20param.20provided.20where.20const.20was.20expected/near/225973745">said</a>:</p>
<blockquote>
<p>i think for the error in <a href="https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47">https://github.com/rust-lang/rust/blob/1efd8049837aec86f21da1486e8a459a7efd7806/compiler/rustc_typeck/src/astconv/generics.rs#L47</a> we can use the param type</p>
</blockquote>
<p>I'm a bit confused because at this point we're still only at the HIR, and the TyKind::Param is on the <code>rustc_middle::TyKind</code> I believe</p>
<p>edit: or did you mean the struct <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamTy.html">ParamTy</a></p>
</blockquote>
<p>we can use <code>tcx.type_of(param.def_id)</code> to get the expected type of <code>N</code> here</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>